from rest_framework.viewsets import GenericViewSet

from  rest_framework import mixins
from django.core.cache import cache
#ListModelMixin列表视图扩展类，提供list(request, *args, **kwargs)方法快速实现列表视图，该Mixin的list方法会对数据进行过滤和分页。
#继承自GenericAPIView，作用也与GenericAPIVIew类似，提供了get_object、get_queryset等方法便于列表视图与详情信息视图的开发。
from . import models,serializers
from utils.response import APIResponse
from settings import offline_config
from rest_framework.throttling import  UserRateThrottle
class BannerListViewSet(mixins.ListModelMixin,GenericViewSet):

    # throttle_classes = [UserRateThrottle]
    #倒序排放,orm查询
    queryset = models.Banner.objects.filter(is_delelte=False,is_show=True).order_by('-order').all()[:offline_config.MAX_COUNT]
    #序列化
    serializer_class=serializers.BannerSerializers

    #自定义群查方法
    def list(self, request, *args, **kwargs):
        data =cache.get("banner_cache")
        #继承list方法，并重写加入状态码
        if not data:
            response = super().list(self, request, *args, **kwargs)
            cache.set('banner_cache',response.data,timeout=7200)
            return  APIResponse(results=response.data)
        return APIResponse(results=data)


